<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="dialogVisible = true"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="roleList">
          <el-table-column label="序号" align="center" type="index" width="120" />
          <el-table-column label="角色名称" align="center" width="240" prop="name" />
          <el-table-column label="描述" align="center" prop="description" />
          <el-table-column label="操作" width="240" fixed="right">
            <template #default="{ row }">
              <el-button size="small" type="primary">分配权限</el-button>
              <el-button size="small" type="info" @click="showDialog(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="params.page"
            :page-size="params.pagesize"
            :total="total"
            layout="prev,pager,next"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <el-dialog :title="roleForm.id ? '编辑角色' : '新增角色'" :visible="dialogVisible" @close="dialogClose">
        <!-- 表单内容 -->
        <el-form ref="roleRef" :model="roleForm" :rules="roleRules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部按钮 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-button size="small" @click="dialogVisible = false">取消</el-button>
          <el-button size="small" type="primary" @click="submit">确定</el-button>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, addRole, getRoleDetail, updateRole, delRole } from '@/api/setting'
export default {
  data() {
    return {
      // 角色列表数据
      roleList: [],
      // 分页参数
      params: {
        page: 1,
        pagesize: 2
      },
      // 总条数
      total: 0,
      dialogVisible: false,
      roleForm: {
        name: '',
        description: ''
      },
      roleRules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchRoleList()
  },
  methods: {
    // 获取角色列表
    async fetchRoleList() {
      const { data: { rows, total }} = await getRoleList(this.params)
      this.roleList = rows
      this.total = total
    },
    handleCurrentChange(newPage) {
      // 将页码赋值当前的最新页码
      this.params.page = newPage
      // 获取最新的表格数据
      this.fetchRoleList()
    },
    // 弹窗关闭
    dialogClose() {
      this.dialogVisible = false
      // 重置表单
      this.$refs.roleRef.resetFields()
      // 重置数据
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    submit() {
      this.$refs.roleRef.validate(async valid => {
        if (!valid) return
        // 2. 调用接口
        // 根据 this.roleForm.id 是否存在，调用不同接口
        if (this.roleForm.id) {
          // 编辑
          await updateRole(this.roleForm)
        } else {
          // 新增
          await addRole(this.roleForm)
        }
        // 3. 提示用户
        this.$message.success(`${this.roleForm.id ? '编辑角色成功' : '新增角色成功'}`)
        // 4. 关闭弹窗
        this.dialogClose()
        // 5. 刷新列表
        this.fetchRoleList()
      })
    },
    // 点击编辑按钮，触发的事件处理函数
    async showDialog(id) {
      // 打开弹窗
      this.dialogVisible = true
      // 获取详情
      const res = await getRoleDetail(id)
      console.log(res)
      this.roleForm = res.data
    },
    // 删除
    delRole(id) {
      // 1. 询问用户
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 2. 调用删除接口
        await delRole(id)
        // 3. 提示用户
        this.$message.success('删除角色成功')
        if (this.roleList.length === 1 && this.params.page > 1) {
          this.params.page--
        }
        // 4. 刷新列表
        this.fetchRoleList()
      })
    }
  }
}
</script>

<style scoped>

</style>
